package com.zebra.gateway.session;

import com.github.benmanes.caffeine.cache.RemovalCause;
import com.github.benmanes.caffeine.cache.RemovalListener;
import com.zebra.common.ExpiredExitMessage;
import com.zebra.common.OfflineType;
import com.zebra.gateway.processor.GatewayProcessorUtil;
import com.zebra.gateway.protocol.GatewaySendMessageUtil;
import com.zebra.remoting.config.BasisCommandCode;
import io.netty.util.internal.logging.InternalLogger;
import io.netty.util.internal.logging.InternalLoggerFactory;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.checker.nullness.qual.Nullable;

public class SessionExpireProcessor implements RemovalListener<Long, Session> {
    private static final InternalLogger logger = InternalLoggerFactory.getInstance(SessionExpireProcessor.class);
    @Override
    public void onRemoval(@Nullable Long key, @Nullable Session value, @NonNull RemovalCause cause){
        if (cause.equals(RemovalCause.EXPIRED)){
            logger.info("Cache Session EXPIRED : key:{}; {}",  key,  value.toString());
            GatewayProcessorUtil.sendTmsInactiveChannel(value, OfflineType.Expired);
            if (value.channelFine()){
                GatewaySendMessageUtil.sendMessage(value.getChannel(), BasisCommandCode.EXPIRED.cmd(),  new ExpiredExitMessage(0, -1L));
            }
        }

    }

}
